﻿using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request;
using Common.Contract.Customer.Response;
using Common.Service.Command;
using Customer.Model.Interface;
using Customer.Model.Model;
using System;
using System.Collections.Generic;

namespace Customer.Core.ServiceCommand
{
    public class RetrieveCustomerInfoByCifServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveCustomerInfoRequest, RetrieveCustomerInfoResponse>
    {
        #region Properties
        public string Cif;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        #endregion
        public RetrieveCustomerInfoByCifServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveCustomerInfoRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (string.IsNullOrEmpty(request.Cif))
            {
                throw new ArgumentException("Cif Customer must be set", "request");
            }
            else
            {
                Cif = Request.Cif;
            }
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
        }

        protected override bool OnExecute(RetrieveCustomerInfoResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            List<CustomerInfoRecord> records = new List<CustomerInfoRecord>();
            Customers customer = new Customers();
            customer = Providers.Customers.RetrieveCustomerByCif(Cif);
            CustomerContact customerContact = Providers.Customers.RetrieveCustomerContactByCif(Cif);
            if (customerContact == null)
            {
                customerContact = new CustomerContact();
            }
            CustomerInfoRecord custInfo = new CustomerInfoRecord
            {
                CustName = customer.CusName,
                Address = customer.Address,
                Email = customer.EmailAddr,
                Phone = customerContact.Mobile,
                LandLine = customerContact.LandLine,
                Office = customerContact.Office,
                Fax = customerContact.Fax,
                VbpIndustry = customer.IndustryName
            };
            records.Add(custInfo);
            response.Records = records;

            return isSuccess;
        }
    }
}
